<?php
class HtmlHelper{
	var $maxSize		= 40;
	var	$defaultSize	= 35;
	
	function link($title, $url, $htmlAttributs=null, $confirmMessage=false, $escapeTitle=true){
		// Generate Link : controller & action
		$arr_url 	= explode('/', $url);
		$url		= "?sys_ctl=".$arr_url[1]."&sys_act=".$arr_url[2];
		
		// Add session id
		if(isset($_GET['sessid'])) {
			$url .= "&amp;sessid=".$_GET['sessid'];
		}
		
		// Add parameter
		$url .= '&param=' . ((isset($arr_url[3])) ? $arr_url[3] : '');
		unset($arr_url);
		
		if($confirmMessage) {
			$htmlAttributs['onclick'] = "return (confirm('". addslashes($confirmMessage) ."'));";
		}
		
		return '<a href="'.$url.'"'.$this -> __renderHtmlAtribut($htmlAttributs).'>'.(($escapeTitle) ? htmlentities($title) : $title).'</a>';
	}
	
	function tagValue($fieldName=''){
		$arr_objName = explode('/', $fieldName);
		$model	= $arr_objName[0];
		$field	= strtolower( $arr_objName[1] );
		$add	= isset($arr_objName[2]) ? strtolower( $arr_objName[2] ) : '';
		unset($arr_objName);
		
		if(trim($add)!='')
			return ((isset( $this -> data[$model][$field][$add]) ) ? $this -> data[$model][$field][$add] : '');
		else
			return ((isset( $this -> data[$model][$field]) ) ? $this -> data[$model][$field] : '');
	}
	
	function input(	$fieldName, 
					$htmlAttributes = array(), 
					$return = false) {
 		/*	Creates a text input widget.

			Parameters:
				string  $fieldNamem Name of a field, like this "Modelname/fieldname"  
				array  $htmlAttributes Array of HTML attributes.  
				boolean  $return Wheter this method should return a value or output it. This overrides AUTO_OUTPUT.  

			Returns:
				mixed Either string or echos the value, depends on AUTO_OUTPUT and $return. public */
		// Untuk Format Uang
		if (isset($htmlAttributes['numeric'])){
			$precision = $htmlAttributes['numeric'];
			unset($htmlAttributes['numeric']);
			
			if (!isset($htmlAttributes['value'])) {
				$htmlAttributes['value'] = number_format( (double)str_replace(',','',$this->tagValue($fieldName)) , $precision, '.', ',');
			}
			
			// event onblur
			if(!isset($htmlAttributes['onblur']))
				$htmlAttributes['onblur'] = 'this.value = formatNumeric(this.value,'.$precision.');';
			else
				$htmlAttributes['onblur'] .= 'this.value = formatNumeric(this.value,'.$precision.');';
				
			// event onfocus
			if(!isset($htmlAttributes['onfocus']))
				$htmlAttributes['onfocus'] = 'this.value = removeCommas(this.value);';
			else
				$htmlAttributes['onfocus'] = 'this.value = removeCommas(this.value);';

			// Set css
			if(!isset($htmlAttributes['style']))
				$htmlAttributes['style'] = "text-align:right;";
		}
		
		if (!isset($htmlAttributes['value'])) { 
			$htmlAttributes['value'] = $this->tagValue($fieldName);
		}
		if (!isset($htmlAttributes['id'])) {
			$htmlAttributes['id'] = renderId($fieldName);
		}
		if (!isset($htmlAttributes['class'])) {
			$htmlAttributes['class'] = 'textfield';
		}
		
		return '<input type="text" name="'.renderName($fieldName).'"'.$this -> __renderHtmlAtribut($htmlAttributes).' />';
 	}
	
	function hidden($fieldName, 
					$htmlAttributes = array(), 
					$return = false) {
 		/*	Creates a text input widget.

			Parameters:
				string  $fieldNamem Name of a field, like this "Modelname/fieldname"  
				array  $htmlAttributes Array of HTML attributes.  
				boolean  $return Wheter this method should return a value or output it. This overrides AUTO_OUTPUT.  

			Returns:
				mixed Either string or echos the value, depends on AUTO_OUTPUT and $return. public */
		if (!isset($htmlAttributes['value'])) {
			$htmlAttributes['value'] = $this->tagValue($fieldName);
		}
		if (!isset($htmlAttributes['id'])) {
			$htmlAttributes['id'] = renderId($fieldName);
		}
		
		return '<input type="hidden" name="'.renderName($fieldName).'"'.$this -> __renderHtmlAtribut($htmlAttributes).' />';
 	}
	
	function file(	$fieldName, 
					$htmlAttributes = array(), 
					$return = false) {
 		
		if (!isset($htmlAttributes['value'])) {
			$htmlAttributes['value'] = $this->tagValue($fieldName);
		}
		if (!isset($htmlAttributes['id'])) {
			$htmlAttributes['id'] = renderId($fieldName);
		}
		
		return '<input type="file" name="'.$fieldName.'"'.$this -> __renderHtmlAtribut($htmlAttributes).' />';
 	}
	
	function password($fieldName, 
					$htmlAttributes = array(), 
					$return = false) {
		/*	Creates a password input widget.

			Parameters:
				string  $fieldName Name of a field, like this "Modelname/fieldname"  
				array  $htmlAttributes Array of HTML attributes.  
				boolean  $return Wheter this method should return a value or output it. This overrides AUTO_OUTPUT.  
			
			Returns:
				mixed Either string or echos the value, depends on AUTO_OUTPUT and $return. public */

		/*if (!isset($htmlAttributes['value'])) {
			$htmlAttributes['value'] = $this->tagValue($fieldName);
		}*/
		if (!isset($htmlAttributes['id'])) {
			$htmlAttributes['id'] = renderId($fieldName);
		}
		
		return '<input type="password" name="'.renderName($fieldName).'"'.$this -> __renderHtmlAtribut($htmlAttributes).' />';
 	}
	
	function textarea(	$fieldName, 
						$htmlAttributes = array(), 
						$return = false) {
		/*	Creates a textarea widget.

			Parameters:
				string  $fieldName Name of a field, like this "Modelname/fieldname"  
				array  $htmlAttributes Array of HTML attributes.  
				boolean  $return Wheter this method should return a value or output it. This overrides AUTO_OUTPUT.  
			
			Returns:
				mixed Either string or echos the value, depends on AUTO_OUTPUT and $return. public */
		if (!isset($htmlAttributes['value'])) {
			$htmlAttributes['value'] = $this->tagValue($fieldName);
		}
		if(isset($htmlAttributes['value'])) {
			$value = $htmlAttributes['value']; unset($htmlAttributes['value']);
		}
		if (!isset($htmlAttributes['id'])) {
			$htmlAttributes['id'] = renderId($fieldName);
		}
		
		return '<textarea name="'.renderName($fieldName).'"'.$this -> __renderHtmlAtribut($htmlAttributes).'>'.$value.'</textarea>';
 	}

	function checkbox(	$fieldName,
						$title = null,
						$htmlAttributes = array(), 
						$return = false) {
		/*	Creates a checkbox widget.

			Parameters:
				string  $fieldName Name of a field, like this "Modelname/fieldname"  
			
			Deprecated:
				string $title 
				Parameters:
				array  $htmlAttributes Array of HTML attributes.  
				boolean  $return Wheter this method should return a value or output it. This overrides AUTO_OUTPUT.  
			
			Returns:
				mixed Either string or echos the value, depends on AUTO_OUTPUT and $return. public */
		if (!isset($htmlAttributes['id'])) {
			$htmlAttributes['id'] = renderId($fieldName);
		}
		
		return '<input type="checkbox" name="'.renderName($fieldName).'"'.$this -> __renderHtmlAtribut($htmlAttributes).' />';
 	}

	function selectTag(	$fieldName, 
						$optionElements, 
						$selected = null, 
						$selectAttr = array(), 
						$optionAttr = null, 
						$showEmpty = true, 
						$return = false) {
		/*	Returns a formatted SELECT element.
		
			Parameters:
				string  $fieldName Name attribute of the SELECT  
				array  $optionElements Array of the OPTION elements (as 'value'=>'Text' pairs) to be used in the SELECT element  
				mixed  $selected Selected option  
				array  $selectAttr Array of HTML options for the opening SELECT element  
				array  $optionAttr Array of HTML options for the enclosed OPTION elements  
				boolean  $show_empty If true, the empty select option is shown  
				boolean  $return Whether this method should return a value  
			
			Returns:
				string Formatted SELECT element public  */
		if (!isset($selected)) {
			$selected = $this->tagValue($fieldName);
		}
		if (!isset($selectAttr['id'])) {
			$selectAttr['id'] = renderId($fieldName);
		}
		if (isset($selectAttr['readonly'])) {
			$showEmpty	= false; $optionElements=array( $selected => $optionElements[$selected] );
		}
		
		$tpl = '<select name="'.renderName($fieldName).'"'.$this -> __renderHtmlAtribut($selectAttr).'>';
			if($showEmpty) $tpl .= '<option value=""></option>';
			$arr_option = ($optionElements==null) ? array() : array_keys($optionElements);
			for($i=0, $max_i=count($arr_option); $i<$max_i; $i++) {
				$ls_key = $arr_option[$i];
				$tpl .= '<option value="'.$ls_key.'"'.(($ls_key==$selected) ? ' selected': '').'>'.$optionElements[$ls_key].'</option>';
			} unset($arr_option);
		$tpl .= '</select>';
		
		return $tpl;
	}
	
	function dtpicker($fieldName, $htmlAttributes = array(), $return = false, $disabledButton=false) {
		/*$this->setFormTag($fieldName);*/
		if (!isset($htmlAttributes['value'])) {
			$htmlAttributes['value'] = $this->tagValue($fieldName);
		}
		
		if (!isset($htmlAttributes['type'])) {
			$htmlAttributes['type'] = 'text';
		}
		
		if (!isset($htmlAttributes['size'])) {
			$htmlAttributes['size'] = '12';
		}
		
		if (!isset($htmlAttributes['maxlength'])) {
			$htmlAttributes['maxlength'] = '10';
		}
		if (!isset($htmlAttributes['class'])) {
			$htmlAttributes['class'] = 'textfield';
		}
		//if (isset($htmlAttributes['readonly'])) {
			//unset($htmlAttributes['readonly']);
		//}
		if($htmlAttributes['class']=='disabled'){
			$htmlAttributes['readonly'] = 'readonly';
		}
		if (!isset($htmlAttributes['onblur'])) {
			//$htmlAttributes['onblur'] = 'convert_date('.renderId($fieldName).')';
			$htmlAttributes['onblur'] = 'convert_date(this)';
		}
		
		if (!isset($htmlAttributes['id'])) {
			$htmlAttributes['id'] = renderId($fieldName);
		}

		/*if ($this->tagIsInvalid($this->model, $this->field)) {
			if (isset($htmlAttributes['class']) && trim($htmlAttributes['class']) != "") {
				$htmlAttributes['class'] .= ' form_error';
			} else {
				$htmlAttributes['class'] = 'form_error';
			}
		}
		
		if(!$disabledButton) {
			return $this->input($fieldName, $htmlAttributes, $return) . '<input name="reset" type="reset" class="dtpickerbtn" onclick="return showCalendar(\''.$htmlAttributes['id'].'\', \'dd-mm-y\');" id="btn_'.$htmlAttributes['id'].'" value="...">';
		}else{
			return $this->input($fieldName, $htmlAttributes, $return) . '<input name="reset" type="reset" class="dtpickerbtn" onclick="return showCalendar(\''.$htmlAttributes['id'].'\', \'dd-mm-y\');" id="btn_'.$htmlAttributes['id'].'" disabled="disabled" value="...">';
		}*/
		return $this->input($fieldName, $htmlAttributes, $return) . '<input name="reset" type="reset" class="btn_calendar" onclick="if(e(\''.$htmlAttributes['id'].'\').className!=\'textfield readonly\') return showCalendar(\''.$htmlAttributes['id'].'\', \'dd-mm-y\');" id="btn_'.$htmlAttributes['id'].'" value="">';
		
	}
	
	function LOV($fieldName, $htmlAttributes, $arr_lov=array(), $disabledLOV=false, $input=true){
		
		//return $this -> LOV($fieldName, $htmlAttributes, $field_info['lov'], $disabledLOV);
		$tpl =	$this -> input($fieldName, $htmlAttributes);
		
		if (!isset($htmlAttributes['id'])) {
			$htmlAttributes['id'] = renderId($fieldName);
		}
		
		if(!$disabledLOV) {
			$tpl .= ' <input type="button" value="..." id="btn_'.$htmlAttributes['id'].'" onclick="popup(e(\''.$htmlAttributes['id'].'\'), \''.$arr_lov['title'].'\', \''.$arr_lov['source'].'\', '.$arr_lov['update'].');" /> <span id="span_'.$htmlAttributes['id'].'"></span>';
							
		}else {
			$tpl .=	' <input type="button" value="..." id="btn_'.$htmlAttributes['id'].'" onclick="if(e(\''.$htmlAttributes['id'].'\').className!=\'disabled\') popup(e(\''.$htmlAttributes['id'].'\'), \''.$arr_lov['title'].'\', \''.$arr_lov['source'].'\', '.$arr_lov['update'].');" /> <span id="span_'.$htmlAttributes['id'].'"></span>';
		}
				
		return $tpl;
	}
	
	function url($str) {
		$arr_str = explode('/', $str);
		$ctl	= isset($arr_str[0]) ? $arr_str[0] : '';
		$act	= isset($arr_str[1]) ? $arr_str[1] : '';
		$param	= '';
		if(count($arr_str)>2) {
			for($i=2, $max_i=count($arr_str); $i<$max_i; $i++) {
				$param .= (($i>2) ? '|' : '') . $arr_str[$i];
			}
		}
		
		return '?sys_ctl='.$ctl.'&sys_act='.$act.'&param='.$param;
	}
	
	function tooltip($title, $tipText, $htmlAttributs=null) {
		return '<a class="tt"'.$this -> __renderHtmlAtribut($htmlAttributs).'>'.$title.'<span class="tooltip"><span class="top"></span><span class="middle">'.$tipText.'</span><span class="bottom"></span></span></a>';
	}
	
	function image($fileName, $htmlAttributs=null) {
		if(!isset($htmlAttributs['alt'])) {
			$htmlAttributs['alt'] = '';
		}
		
		return '<img src="' + base_url() + 'content/images/'.$fileName.'"'.$this -> __renderHtmlAtribut($htmlAttributs)." />";
	}
	
	function captcha($arr_val, $requestId='') {
		return '<div id="captchaContainer"><span class="cover">&nbsp;</span><span class="char0">'.$arr_val[0].'</span><span class="char1">'.$arr_val[1].'</span><span class="char2">'.$arr_val[2].'</span><span class="char3">'.$arr_val[3].'</span><span class="char4">'.$arr_val[4].'</span></div><input type="hidden" name="requestId" id="requestId" value="'.$requestId.'" />';
	}
	
	function generate_pagination_link($url, $currentpage, $numpage) {
		$tpl = '';
		if($numpage<8){
			for($i=1; $i<=$numpage; $i++){
				if($i==$currentpage) {
					$tpl .= '<b>['.$i.']</b>';
				}else{
					$tpl .= $this -> link(	'['.$i.']',
											$url.$i,
											array('loading'=>'true', 'class'=>'paginationLink'));
				}
				$tpl .= '&nbsp;';
			}
		}else{
			for($i=1; $i<=$numpage; $i++){
				if($i==$currentpage) {
					$tpl .= '<b>['.$i.']</b>';
				}else
				if($i==1 || $i==2)
				{
					$tpl .= $this -> link(	'['.$i.']',
											$url.$i,
											array('loading'=>'true', 'class'=>'paginationLink'));
				}else
				if($i==($currentpage-1) || $i==($currentpage-2) || $i==($currentpage+1) || $i==($currentpage+2))
				{
					$tpl .= $this -> link(	'['.$i.']',
											$url.$i,
											array('loading'=>'true', 'class'=>'paginationLink'));
				}else
				if($i==($numpage-1) || $i==$numpage)
				{
					$tpl .= $this -> link(	'['.$i.']',
											$url.$i,
											array('loading'=>'true', 'class'=>'paginationLink'));
				}else{
					if($i==3){
						$tpl .= '..';
					}
					if($i==($numpage-2)) {
						$tpl .= '..';
					}
				}
				
				$tpl .= ' ';
			}
		}
		
		return $tpl;
	}
	
	function autofield($fieldName, $field_info, $htmlAttributesUser=NULL, $arr_config=NULL){
		if($arr_config==NULL) $arr_config = array();
		if($htmlAttributesUser==NULL) $htmlAttributesUser = array();
		
		$fieldType		= $field_info['field_type'];
		$fieldLength	= $field_info['field_size'];
		$readonly		= !array_key_exists('readonly', $arr_config) ? false : (in_array($fieldName, $arr_config['readonly']) ) ;
		$fieldValue		= $this -> tagValue($fieldName);
		
		// Set Input Size
		$maxSize		= $this -> maxSize;
		$defaultSize	= $this -> defaultSize;
		if($fieldLength=='' || !is_numeric($maxSize))		$ln_size = $defaultSize;
		else if(intval($fieldLength) > intval($maxSize))	$ln_size = $maxSize;
		else												$ln_size = $fieldLength;
		
		
		if(isset($field_info['lov']) && is_array($field_info['lov']) && count($field_info['lov'])>0){
			
			$htmlAttributes = array_merge($htmlAttributesUser, array('class'=>'input', 'size'=>$ln_size, 'value'=>$fieldValue));
			if(intval($fieldLength)>0) $htmlAttributes['maxlength'] = $fieldLength;
			$disabledLOV = false;
			if($readonly || $htmlAttributesUser['readonly'] == 'readonly'){ 
				$htmlAttributes['readonly'] = 'readonly'; 
				$htmlAttributes['class'] = 'disabled'; 
				$disabledLOV=true; 
			}
			$htmlAttributes	= array_merge($htmlAttributes, $htmlAttributesUser);
			return $this -> LOV($fieldName, $htmlAttributes, $field_info['lov'], $disabledLOV);

		}else
		if(isset($field_info['field_option']) && is_array($field_info['field_option']) && count($field_info['field_option'])>0){
			
			$htmlAttributes = array_merge($htmlAttributesUser, array('class'=>'input'));
			if($readonly || $htmlAttributesUser['readonly'] == 'readonly'){ $htmlAttributes['readonly'] = 'readonly'; $htmlAttributes['class'] = 'disabled'; }
			$htmlAttributes	= array_merge($htmlAttributes, $htmlAttributesUser);
			return $this -> selectTag($fieldName, $field_info['field_option'], $fieldValue, $htmlAttributes, NULL, TRUE, TRUE);
			
		}else
		if($fieldType == 'string' && ($fieldLength<0 || $fieldLength>255)){
						
			$htmlAttributes = array('class'=>'input', 'cols'=>($this -> maxSize -3), 'rows'=>'4', 'value'=>$fieldValue);
			if($readonly || $htmlAttributesUser['readonly'] == 'readonly'){ $htmlAttributes['readonly'] = 'readonly'; $htmlAttributes['class'] = 'disabled'; }
			if(intval($fieldLength)>0) $htmlAttributes['maxlength'] = $fieldLength;
			$htmlAttributes	= array_merge($htmlAttributes, $htmlAttributesUser);
			return $this -> textarea($fieldName, $htmlAttributes, true);
			
		}else
		if($fieldType == 'string'){
						
			$htmlAttributes = array('class'=>'input', 'size'=>$ln_size, 'value'=>$fieldValue);
			if($readonly || $htmlAttributesUser['readonly'] == 'readonly'){ $htmlAttributes['readonly'] = 'readonly'; $htmlAttributes['class'] = 'disabled'; }
			if(intval($fieldLength)>0) $htmlAttributes['maxlength'] = $fieldLength;
			$htmlAttributes	= array_merge($htmlAttributes, $htmlAttributesUser);
			return $this -> input($fieldName, $htmlAttributes, true);
			
		}else
		if($fieldType == 'numeric'){

			$fieldValue	= number_format( str_replace(',','',$fieldValue), 2, '.', ',');
			if($ln_size<$this->minSize) $ln_size = $this->minSize;
			$arr_scale = explode('.',$fieldLength); $len_nom = intval($arr_scale[0]); $len_dec = intval($arr_scale[1]); unset($arr_scale);
			$htmlAttributes = array('class'=>'input', 'size'=>$ln_size, 'value'=>$fieldValue, 'style'=>'text-align:right;', 'onblur'=>'this.value=formatNumeric(this.value, '.$len_dec.');', 'onfocus'=>'this.value=removeCommas(this.value);');
			if($readonly || $htmlAttributesUser['readonly'] == 'readonly'){ $htmlAttributes['readonly'] = 'readonly'; $htmlAttributes['class'] = 'disabled'; }
			$htmlAttributes	= array_merge($htmlAttributes, $htmlAttributesUser);
			$htmlAttributes['style'] .= 'text-align:right;';
			return $this -> input($fieldName, $htmlAttributes, true);
			
		}else
		if($fieldType == 'time' && $fieldLength==10){
			
			$htmlAttributes = array('class'=>'input', 'size'=>$ln_size, 'maxlength'=>$fieldLength, 'value'=>$fieldValue);
			if($readonly || $htmlAttributesUser['readonly'] == 'readonly'){ $htmlAttributes['readonly'] = 'readonly'; $htmlAttributes['class'] = 'disabled'; }
			$htmlAttributes	= array_merge($htmlAttributes, $htmlAttributesUser);
			return $this -> dtpicker($fieldName, $htmlAttributes, true);
			
		}else
		if($fieldType == 'time'){
			
			$htmlAttributes = array('class'=>'input', 'size'=>$ln_size, 'value'=>$fieldValue);
			if($readonly || $htmlAttributesUser['readonly'] == 'readonly'){ $htmlAttributes['readonly'] = 'readonly'; $htmlAttributes['class'] = 'disabled'; }
			if(intval($fieldLength)>0) $htmlAttributes['maxlength'] = $fieldLength;
			$htmlAttributes	= array_merge($htmlAttributes, $htmlAttributesUser);
			return $this -> input($fieldName, $htmlAttributes, true);
			
		}
	}
	
	
	function action_button($arr_param, $url){
		if(count($arr_param)>0) {
			$arr_keys = array_keys($arr_param);
		}
		
		echo '<div class="action_bar"><ul>';
		for($i=0,$max_i=count($arr_keys); $i<$max_i; $i++){
			$key	= $arr_keys[$i];
			
			if($key	== 'add'){ 
				if(!isset($arr_param[$key]['title']) || isset($arr_param[$key]['title'])==''){
					$arr_param[$key]['title']	= 'Tambah';  }
				if(!isset($arr_param[$key]['icon']) || isset($arr_param[$key]['icon'])=='')
					$arr_param[$key]['icon']	= 'ico_add.png';
				if(!isset($arr_param[$key]['action']) || isset($arr_param[$key]['action'])=='')
					$arr_param[$key]['action']	= 'window.location = \''.$url.'&mode=add\';';
			}else
			if($key	== 'edit'){
				if(!isset($arr_param[$key]['title']))
					$arr_param[$key]['title']	= 'Ubah';
				if(!isset($arr_param[$key]['icon']))
					$arr_param[$key]['icon']	= 'ico_edit.png';
				if(!isset($arr_param[$key]['action']))
					$arr_param[$key]['action']	= 'if(e(\'current_id\').value == \'\') alert(\'Silahkan pilih baris yang akan diubah\'); else window.location = \''.$url.'&mode=edit&id=\'+e(\'current_id\').value;';
			}else
			if($key	== 'delete'){
				if(!isset($arr_param[$key]['title']))
					$arr_param[$key]['title']	= 'Hapus';
				if(!isset($arr_param[$key]['icon']))
					$arr_param[$key]['icon']	= 'ico_delete.png';
				if(!isset($arr_param[$key]['action']))
					$arr_param[$key]['action']	= 'if(e(\'current_id\').value == \'\') alert(\'Silahkan pilih baris yang akan dihapus\'); else{ if(confirm(\'Anda yakin akan menghapus data?\')){ window.location = \''.$url.'&mode=delete&id=\'+e(\'current_id\').value; }}';
			}
			if($key	== 'save'){
				if(!isset($arr_param[$key]['title']))
					$arr_param[$key]['title']	= 'Simpan';
				if(!isset($arr_param[$key]['icon']))
					$arr_param[$key]['icon']	= 'ico_save.png';
				if(!isset($arr_param[$key]['action']))
					$arr_param[$key]['action']	= 'document.fm_detil.submit();';
			}
			if($key	== 'view'){
				if(!isset($arr_param[$key]['title']))
					$arr_param[$key]['title']	= 'Tampil';
				if(!isset($arr_param[$key]['icon']))
					$arr_param[$key]['icon']	= 'ico_refresh.png';
				if(!isset($arr_param[$key]['action']))
					$arr_param[$key]['action']	= 'window.location = \''.$url.'\';';
			}
			
			echo '<li><a href="#" onclick="'.$arr_param[$key]['action'].'return false;" title="'.$arr_param[$key]['title'].'"><img src="includes/images/'.$arr_param[$key]['icon'].'" border="0" alt="'.$arr_param[$key]['title'].'" /> '.$arr_param[$key]['title'].'</a></li>';
		}
		echo '</ul><br clear="all" /></div>';
	}
	
	function generate_grid($arr_fields, $result, $arr_setting=array()){
		$arr_hide		= $arr_setting['hidden'];
		$arr_primary	= $arr_setting['unique'];
		
		if(array_key_exists('label', $arr_setting) && count($arr_setting['label'])>0){
			$arr_keys = array_keys($arr_setting['label']);
			$lb_label	= true;
		}else
		if(count($arr_fields)>0){
			$arr_keys = array_keys($arr_fields);
			$lb_label	= false;
		}
		
		// Header
		if(array_key_exists('header', $arr_setting) && $arr_setting['header']){
			$ln_visisbleColumn = 0;
			echo '<tr>';
			
			if(array_key_exists('numbering', $arr_setting) && $arr_setting['numbering']){
				echo '<th>No.</th>';
			}
			
			for($i=0,$max_i=count($arr_keys); $i<$max_i; $i++){
				$key = $arr_keys[$i];
				
				if(!in_array($key,$arr_hide)){
					if($lb_label){
						$label	= $arr_setting['label'][$key];
					}else{
						$label	= ucfirst(str_replace('_', ' ',$key));
					}
					
					echo '<th>' . $label . '</th>';
					$ln_visisbleColumn++;
				}
				
			}
			echo '</tr>';
			echo '<tr>'; for($i=1; $i<=$ln_visisbleColumn; $i++){ echo '<th>('.$i.')</th>'; } echo '</tr>';
		}
		
	    for($i=0, $max_i=count($result); $i<$max_i; $i++){
			// Get primary key
			$id	= '';
			for($j=0,$max_j=count($arr_primary); $j<$max_j; $j++){
				$key = strtolower($arr_primary[$j]);
				$id	.= ($j!=0 ? '|' : '') . $result[$i][$key];
			}
			
			// Loop row
			echo '<tr id="row_'.$id.'" onclick="row_click(\''.$id.'\');">';
				
				if(array_key_exists('numbering', $arr_setting) && $arr_setting['numbering']){
					echo '<td>'.($i+1).'</td>';
				}
			
				// Loop column
				for($j=0,$max_j=count($arr_keys); $j<$max_j; $j++){
					$key = $arr_keys[$j];
					
					if(isset($arr_hide)){
						if(!in_array($key,$arr_hide)){
							
							if(isset($arr_fields[$key]['field_option']) && is_array($arr_fields[$key]['field_option']) ){
								echo '<td>'.$arr_fields[$key]['field_option'][$result[$i][$key]].'</td>';
							}else
							if($arr_fields[$key]['field_type']	== 'numeric'){
								echo '<td style="text-align:right;">' . number_format($result[$i][$key],0,'.',',') . '</td>';
							}else{
								echo '<td>'.$result[$i][$key].'</td>';
							}
							
						}
					}else{
						
							if(isset($arr_fields[$key]['field_option']) && is_array($arr_fields[$key]['field_option']) ){
								echo '<td>'.$arr_fields[$key]['field_option'][$result[$i][$key]].'</td>';
							}else
							if($arr_fields[$key]['field_type']	== 'numeric'){
								echo '<td style="text-align:right;">' . number_format($result[$i][$key],0,'.',',') . '</td>';
							}else{
								echo '<td>'.$result[$i][$key].'</td>';
							}
						
					}
				}

			
			echo '</tr>';

		}
	}
	
	function __renderHtmlAtribut($attr = null) {
		$tpl = '';
		if(isset($attr)) {
			$arr_attrName	= array_keys($attr);
			$arr_attrVal	= array_values($attr);
			$ln_jml			= count($attr);
			for($i=0; $i<$ln_jml; $i++) {
				$tpl .= ' '.$arr_attrName[$i].'="'.$arr_attrVal[$i].'"';
			}
			unset($arr_attrName, $arr_attrVal, $ln_jml);
		}
		
		return $tpl;
	}
}
?>